package app.imttf.internal.bean;

import javax.faces.event.ValueChangeEvent;
import java.util.ArrayList;

import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCIteratorBinding;

import oracle.binding.BindingContainer;

import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;
import oracle.jbo.domain.Number;
import oracle.jbo.uicli.binding.JUCtrlListBinding;

public class RelocateRole {

    private Integer[] selectedIndxs;
    private ArrayList<Integer> selectedIndxList;

    public RelocateRole() {
    }

    public void setSelectedIndxs(Integer[] selectedIndxs) {
        this.selectedIndxs = selectedIndxs;
    }

    //method referenced from the value property of the Select Many Shuttle
    //component. Note that a related setter method is contained in this managed
    //bean as well
    public Integer[] getSelectedIndxs() {
      selectedIndxs = null;
      selectedIndxList = new ArrayList<Integer>();
      
      BindingContext bctx = BindingContext.getCurrent();
      BindingContainer bindings = bctx.getCurrentBindingsEntry();
      //get all employees for the current selected department
      DCIteratorBinding userInRoleDciter = (DCIteratorBinding)bindings.get("userInRoleIterator");          
      RowSetIterator rsi = userInRoleDciter.getRowSetIterator();
      //get access to the "allEmployees" list binding
      JUCtrlListBinding allRolesList  = (JUCtrlListBinding)bindings.get("RolesView12");
      DCIteratorBinding allRolesIter = allRolesList.getDCIteratorBinding();
      
      while(rsi.hasNext()){
         //iterate over the employees in the selected department and find the 
         //employee record index in the allEmployees iterator. Note that the
        //employee in the allEmployees iterator has a different index number 
        //than the same employee record in the department's dependent View Object 
         Row rw = (Row) rsi.next();
         allRolesIter.setCurrentRowWithKey(rw.getKey().toStringFormat(true));
         int indx = allRolesIter.getCurrentRowIndexInRange();
         selectedIndxList.add(indx);
      }
      
      selectedIndxs = selectedIndxList.toArray(new Integer[selectedIndxList.size()]);
        return selectedIndxs;
    }

    //to-do in this method: get the DepartmentsId and update the 
    //employees added to the department
    public void onRoleChange(ValueChangeEvent valueChangeEvent) {
        // Add event code here...
        // Add event code here...
        //get all items left as selected after a change
        System.out.println("TEST");
        Integer[] userInRole = (Integer[]) valueChangeEvent.getNewValue();
        //get the DepartmentId
        BindingContext bctx = BindingContext.getCurrent();
        BindingContainer bindings = bctx.getCurrentBindingsEntry();
        DCIteratorBinding allUsersIterator = (DCIteratorBinding)bindings.get("UserAccessListView2Iterator");   
        //get current row
        Row currentRow = allUsersIterator.getCurrentRow();
        Number userName = (Number) currentRow.getAttribute("UserName");
        
        //compare the selected values in the list and update all rows that don't 
        //yet have the department Id of the selected department
        
        JUCtrlListBinding allRolesList  = (JUCtrlListBinding)bindings.get("RolesView12");
        DCIteratorBinding allRolesIter = allRolesList.getDCIteratorBinding();
        
        System.out.println("userInRole= " + userInRole.length);

        if (userInRole.length > 0) {
            for(int index : userInRole){
              Row rowToUpdate = allRolesIter.getRowAtRangeIndex(index);
                if (!((Number)rowToUpdate.getAttribute("UserName")).equals(userName)){
                 rowToUpdate.setAttribute("UserName", userName);
                }
            }
            //Note that this sample does not commit the changes. I do this on 
            //purpose not to mess with your database.
        }
    }
}
